<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Gregwar\Captcha\CaptchaBuilder;
use Session;
use App\Model\User;
use Hash;
use Mail;
use App\Http\Requests\RegisterRequest;

class homeRegisterController extends Controller
{
    //
    public function register()
    {
    	return view('home.register');
    }

    public function postRegister(RegisterRequest $request)
    {
    	
    	//检测用户名是否已被使用
    	$user = User::where('email',$request->email)->first();	
    	if($user){
    		return back()->with('error','邮箱已存在');
    	}
    	//处理数据
    	$data = $request->except(['_token','repassword']);
    	$data['password']=Hash::make($data['password']);
    	$data['token']=str_random(50);//用来激活邮箱
    	$data['status']=0;
    	$data['create_at']=date('Y-m-d',time());

    	$user = User::create($data);

    	if($user){
    		$this->sendMail($user->id,$data['token'],$data['email']);
    		return redirect('/')->with('success','注册成功');
    	}else{
    		return back()->with('error','注册失败');
    	}
    	//匹配规则
    	preg_match('/@(.*)\./',$request->email,$temp);
    	//邮箱格式
	 	$eml='';
        switch($temp[1]){
                case 'qq':
                $eml='https://mail.qq.com/';
                break; 
                case '163':
                $eml='http://mail.163.com/';
                break;
                case '126':
                $eml ='www.126.com';
                case '139':
                $eml ='http://mail.10086.cn/';
                default:
                $eml='';
                break;
            }
    }


    //发送邮件
    public function sendMail($id,$token,$email)
    {
    	    Mail::send('home.email',['id'=>$id,'token'=>$token],function($message)use($email){
             //发送地址
             $message->to($email);
             //邮件标题
             $message->subject('激活邮件');
    });
 
    }

    //邮件激活
    public function mailActivation(Request $request)
    {


    	$inptoken = $request->input('token');
    	
    	$dbtoken = User::where('id',$request['id'])->first()['token'];
   
    	if($inptoken == $dbtoken){
    		
			//更改数据库中的状态为1
			if(User::where('id',$request->input('id'))->update(['status'=>1])){
				//更新token
				$newtoken = str_random(50);
				User::where('id',$request->input('id'))->update(['token'=>$newtoken]);
                
				return redirect('/homeRegister');
			}else{
				return back();
			}
		
    	}else{
    		return '非法请求@';
    	}
    	
    }


    //验证码
    public function code()
    {
    	//生成验证码图片的Builder对象，配置相应属性
        $builder = new CaptchaBuilder;
     
        //可以设置图片宽高及字体
        $builder->build($width = 100, $height = 40, $font = null);
        //获取验证码的内容
        $phrase = $builder->getPhrase();
       
        //将session写入闪存
        session(['code'=>$phrase]);
        //生成图片
        header("Cache-Control: no-cache, must-revalidate");
        header('Content-Type: image/jpeg');
        $builder->output();
    }


    //执行前台登录
    public function postLogin(Request $request)
    {
    	
    	//检测验证码是否一致
    	if(session('code')!=$request->code){
    		return back()->with('error','验证码错误!');
    	}
    	//检测用户名是否存在
    	$user = User::where('email',$request->email)->first();

    	$userpass = Hash::make($user->password);

	 	//检测密码是否一致
	 	if(Hash::check($user->password,$userpass)){
	 		session(['homeInfo'=>$user]);
           
	 		if($user->status ==1){
	 			//备注带了id
	 			return redirect('/')->with('id',$user->id);
	 		}elseif($user->status ==2){
	 		return view('/news');
	 		}
	 	}else{
	 		return back();
	 		
	 	}	
    }


    //执行用户退出
    public function logout()
    {
    	session()->forget('homeInfo');
 		return redirect('/');
    }
}
